package com.ruoyi.park.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.park.domain.WySupplier;
import com.ruoyi.park.service.IWySupplierService;
import com.ruoyi.system.api.RemoteBmCodeService;
import com.ruoyi.system.api.domain.RemoteSysBmType;
import com.ruoyi.system.api.enums.SysBmCodeTypeValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 供应商Controller
 *
 * @author cjie
 * @date 2021-12-04
 */
@Api(tags = " 供应商")
@RestController
@RequestMapping("/wySupplier")
public class WySupplierController extends BaseController {
    @Autowired
    private IWySupplierService wySupplierService;

    @Autowired
    private RemoteBmCodeService remoteBmCodeService;

    /**
     * 查询供应商列表
     */
    @ApiOperation(value="查询供应商列表", notes="查询供应商列表")
    @PreAuthorize(hasPermi = "park:wySupplier:list")
    @GetMapping("/list")
    public TableDataInfo list(WySupplier wySupplier) {
        startPage();
        List<WySupplier> list = wySupplierService.selectWySupplierList(wySupplier);
        return getDataTable(list);
    }

    /**
     * 导出供应商列表
     */
    @ApiOperation(value="导出供应商列表", notes="导出供应商列表")
    @PreAuthorize(hasPermi = "park:wySupplier:export")
    @Log(title = "供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WySupplier wySupplier) throws IOException {
        List<WySupplier> list = wySupplierService.selectWySupplierList(wySupplier);
        ExcelUtil<WySupplier> util = new ExcelUtil<WySupplier>(WySupplier.class);
        util.exportExcel(response, list, "供应商数据");
    }
    /**
     * 获取供应商详细信息
     */
    @ApiOperation(value="获取供应商详细信息", notes="获取供应商详细信息")
    @PreAuthorize(hasPermi = "park:wySupplier:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(wySupplierService.selectWySupplierById(id));
    }

    /**
     * 新增供应商
     */
    @ApiOperation(value="新增供应商", notes="新增供应商")
    @PreAuthorize(hasPermi = "park:wySupplier:add")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WySupplier wySupplier) {
//        RemoteSysBmType remoteSysBmType= remoteBmCodeService.genCode(wySupplier.getSysBmId(), SysBmCodeTypeValue.DEVICE);
//        String nowCode = remoteSysBmType.getNowCode();//生成的唯一编码
//        String deviceType = remoteSysBmType.getHierarchy();//层级
//        wySupplier.setNum(nowCode);
//        wySupplier.setType(deviceType);
        return toAjax(wySupplierService.insertWySupplier(wySupplier));
    }

    /**
     * 修改供应商
     */
    @ApiOperation(value="修改供应商", notes="修改供应商")
    @PreAuthorize(hasPermi = "park:wySupplier:edit")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WySupplier wySupplier) {
        return toAjax(wySupplierService.updateWySupplier(wySupplier));
    }

    /**
     * 删除供应商
     */
    @ApiOperation(value="删除供应商", notes="删除供应商")
    @ApiImplicitParam(name = "ids", value = "供应商ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "park:wySupplier:remove")
    @Log(title = "供应商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(wySupplierService.deleteWySupplierByIds(ids));
    }
}
